<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 消息推送管理
 */
class Pushsms extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->notice_type_cf = $this->webconf['MSGTYPE'];//消息类型配置
    }
    //推送消息列表页
    public function index()
    {
        //分页
       $getcount = $this->db->query("select count(*) num from tan_push_content order by id desc")->row_array();
       $getcount = $getcount['num'];
       $navigation_position = 'outside';
       $records_per_page = 10;
       $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
       //$this->xpage->reverse(true);
       $this->xpage->records($getcount);
       $this->xpage->records_per_page($records_per_page);
       $this->xpage->labels("<<", ">>");
       $list = $this->db->query("select * from tan_push_content order by id desc limit ".(($this->xpage->get_page() - 1) * $records_per_page) .",".$records_per_page)->result_array();
       foreach ($list as $key =>$val){
           $list[$key]['type'] = $val['type'] == 2 ? "活动通知" : ($val['type'] == 4 ? "公告通知" : ($val['type'] == 6 ? "贷款平台通知":"其他通知"));
           $list[$key]['push_time'] = $val['push_time'] ? date("Y-m-d H:i",$val['push_time']) : "--/--";
           $list[$key]['target_app'] = $val['target_app'] == "tan66" ? "弹溜溜": "溜溜钱包";
       }
       
       $this->template->assign('Total',$getcount);
       $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p','index');
        $this->template->load('pushsms/index');
    }
    
    //创建推送消息
    public function add(){
        
        if($_POST){
            $act_type = trim($_POST['act_type']);
            $title = trim($_POST['title']);
            $content = trim($_POST['content']);
            $target_id = trim($_POST['target_id']);
            $target_app = isset($_POST['target_app']) ?  trim($_POST['target_app']) : "tan66";
            $user_ids = trim($_POST['user_ids']);
            $type = trim($_POST['type']);
            $user_type = trim($_POST['user_type']);//1为全体用户，0为指定用户
            if(!$type){
                $mssage['info']	  = '请选择消息类型';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$title){
                $mssage['info']	  = '消息标题不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$content){
                $mssage['info']	  = '消息内容不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$target_id){
                $mssage['info']	  = '消息ID不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$user_type && !$user_ids){
                $mssage['info']	  = '指定用户id不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['title'] = $title;
            $data['content'] = $content;
            $data['target_id'] = $target_id;
            $data['target_app'] = $target_app;
            $data['status'] = 0;
            $data['type'] = $type;
            $data['user_ids'] = $user_ids;
            $data['create_time'] = time();
            
            $this->db->insert("tan_push_content",$data);
            $msg_id = $this->db->insert_id();
            
            if($msg_id){
                if($act_type){//立即推送
                    $push_data = array(
                        "title" => $title,
                        "content" => $content,
                        "type" => $type,
                        "message_id" => $target_id
                    );
                    $usersAliasList = array();
                    if($user_ids){
                        $user_ids = explode(",", $user_ids);
                        foreach ($user_ids as $id){
                            $str .= encrypt_user_id($id).",";
                        }
                        $str = rtrim($str,",");
                        $usersAliasList = explode(",", $str);
                    }
                    //开始推送，并获取推送结果
                    $res = $user_ids ? insert_push_to_list($usersAliasList, $push_data, $type,$target_app) : insert_push_to_app($push_data, $type,$target_app);
                    if($res['result'] == "ok"){
                        $this->db->where("id",$msg_id)->update("tan_push_content",array("status"=>1,"push_time"=>  time(),"extra"=>  serialize($res)));
                        $mssage['info']	  = '推送成功';
                        $mssage['status'] = 1;
                        $mssage['url']	  = site_url('pushsms/index');
                        echo json_encode($mssage);exit;
                    }else{
                        $this->db->where("id",$msg_id)->update("tan_push_content",array("status"=>2,"push_time"=>  time(),"extra"=>  serialize($res)));
                        $mssage['info']	  = '推送失败';
                        $mssage['status'] = 0;
                        $mssage['url']	  = site_url('pushsms/index');
                        echo json_encode($mssage);exit;
                    }
                    
                }else{
                    $mssage['info']	  = '创建成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('pushsms/index');
                    echo json_encode($mssage);exit;
                }
            }else{
                $mssage['info']	  = '创建失败，请稍后再试';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('pushsms/index');
                echo json_encode($mssage);exit;
            }
        }
        
        $this->template->assign('segment_p','index');
        $this->template->load('pushsms/add');
    }
    
    //修改推送消息
    public function edit(){
        if($_POST){
            $msg_id = trim($_POST['sms_id']);
            $act_type = trim($_POST['act_type']);
            $title = trim($_POST['title']);
            $content = trim($_POST['content']);
            $target_id = trim($_POST['target_id']);
             $target_app = isset($_POST['target_app']) ?  trim($_POST['target_app']) : "tan66";
            $user_ids = trim($_POST['user_ids']);
            $type = trim($_POST['type']);
            $user_type = trim($_POST['user_type']);//1为全体用户，0为指定用户
            if(!$type){
                $mssage['info']	  = '请选择消息类型';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$title){
                $mssage['info']	  = '消息标题不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$content){
                $mssage['info']	  = '消息内容不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$target_id){
                $mssage['info']	  = '消息ID不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$user_type && !$user_ids){
                $mssage['info']	  = '指定用户id不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['title'] = $title;
            $data['content'] = $content;
            $data['target_id'] = $target_id;
            $data['status'] = 0;
            $data['type'] = $type;
            $data['user_ids'] = $user_ids;
            $data['create_time'] = time();
            
            $result = $this->db->where("id",$msg_id)->update("tan_push_content",$data);
            
            if($result){
                if($act_type){//立即推送
                    $push_data = array(
                        "title" => $title,
                        "content" => $content,
                        "type" => $type,
                        "message_id" => $target_id
                    );
                    $usersAliasList = array();
                    if($user_ids){
                        $user_ids = explode(",", $user_ids);
                        foreach ($user_ids as $id){
                            $str .= encrypt_user_id($id).",";
                        }
                        $str = rtrim($str,",");
                        $usersAliasList = explode(",", $str);
                    }
                    //开始推送，并获取推送结果
                    $res = $user_ids ? insert_push_to_list($usersAliasList, $push_data, $type,$target_app) : insert_push_to_app($push_data, $type,$target_app);
                    if($res['result'] == "ok"){
                        $this->db->where("id",$msg_id)->update("tan_push_content",array("status"=>1,"push_time"=>  time(),"extra"=>  serialize($res)));
                        $mssage['info']	  = '推送成功';
                        $mssage['status'] = 1;
                        $mssage['url']	  = site_url('pushsms/index');
                        echo json_encode($mssage);exit;
                    }else{
                        $this->db->where("id",$msg_id)->update("tan_push_content",array("status"=>2,"push_time"=>  time(),"extra"=>  serialize($res)));
                        $mssage['info']	  = '推送失败';
                        $mssage['status'] = 0;
                        $mssage['url']	  = site_url('pushsms/index');
                        echo json_encode($mssage);exit;
                    }
                    
                }else{
                    $mssage['info']	  = '创建成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('pushsms/index');
                    echo json_encode($mssage);exit;
                }
            }else{
                $mssage['info']	  = '创建失败，请稍后再试';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('pushsms/index');
                echo json_encode($mssage);exit;
            }
        }
        $id = isset($_REQUEST['id']) ? trim($_REQUEST['id']) : 0;
        $item = $this->db->query("select * from tan_push_content where id = $id order by id desc limit 1")->row_array();
        if(!$item){
            redirect("pushsms/index");
        }
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','index');
        $this->template->load('pushsms/edit');
    }
    
}
